package com.atguigu.srb.core.controller.admin;

import com.atguigu.common.exception.Assert;
import com.atguigu.common.result.R;
import com.atguigu.common.result.ResponseEnum;
import com.atguigu.srb.core.pojo.entity.IntegralGrade;
import com.atguigu.srb.core.service.IntegralGradeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Api(tags = "积分等级管理")
@CrossOrigin
@RestController
@RequestMapping("/admin/core/integralGrade")
public class AdminIntegralGradeController {
    @Resource
    private IntegralGradeService integralGradeService;

    /*获取数据列表*/
    @GetMapping("/list")
    @ApiOperation("查询所有积分等级列表")
    public R listAll() {
        List<IntegralGrade> list = integralGradeService.list();
        return R.ok().data("list", list).message("获取列表成功");
    }

    // swagger2中的value说明，notes更为详细的说明
    @ApiOperation(value = "根据Id删除积分记录", notes = "逻辑删除积分记录")
    @DeleteMapping("/remove/{id}")
    public R removeById(@ApiParam(value = "数据id", example = "1", required = true)
                                  @PathVariable Long id) { // ApiParam：解释swagger中id的含义
        boolean removeById = integralGradeService.removeById(id);
        if (removeById) {
            return R.ok().message("删除成功");
        } else {
            return R.error().message("删除失败");
        }
    }

    @ApiOperation("新增积分等级")
    @PostMapping("/save")
    public R save(
            @ApiParam(value = "积分等级对象", required = true)
            @RequestBody IntegralGrade integralGrade) {
                Assert.notNull(integralGrade.getBorrowAmount(), ResponseEnum.BORROW_AMOUNT_NULL_ERROR);

                boolean save = integralGradeService.save(integralGrade);
                if (save) {
                    return R.ok().message("新增积分等级成功");
                } else {
                    return R.error().message("新增积分等级失败");
                }
    }


    /*
    * 根据id查询积分等级
    * */
    @ApiOperation("根据id获取积分等级")
    @GetMapping("/get/{id}")
    public R getById(@ApiParam(value = "数据id", required = true) @PathVariable Long id) {
        IntegralGrade byId = integralGradeService.getById(id);
        if (byId != null) {
            return R.ok().data("record", byId);
        } else {
            return R.error().message("查询积分等级失败");
        }
    }

    @ApiOperation("更新积分等级")
    @PutMapping("/update")
    public R update(
            @ApiParam(value = "积分等级对象", required = true)
            @RequestBody IntegralGrade integralGrade
    ) {
        boolean updateById = integralGradeService.updateById(integralGrade);
        if (updateById) {
            return R.ok().message("更新成功");
        } else {
            return R.error().message("更新失败");
        }
    }
}